<?php

namespace QF;

class User extends \Model {
    
    use \UserModel;
    
    static public $table = 'user';
    protected $_fillable = [
        'uid' => 'int',
        'username' => 'str',
        'realname' => 'str',
        'idcard' => 'str',
        'password' => 'str',
        'nickname' => 'str',
        'sex' => 'int',
        'birthday' => 'datetime',
        'mobile' => 'str',
        'email' => 'str',
        'avatar' => 'str',
        'locked' => 'int',
        'language' => 'str',
    ];
    protected $_id = 'uid';
    private $_md5key;
    
    public function __construct() {
        $this->_md5key = \Sys::getCfg('user_crypt_key');
    }
    
    public function add($data) {
        if (!$data['nickname']) {
            $data['nickname'] = filter_utf8mb4($data['username']);
        } else {
            $data['nickname'] = filter_utf8mb4($data['nickname']);
        }
        
        if ($data['password']) {
            $data['password'] = cmd5($data['password'], $this->_md5key);
        }
        
        return parent::add($data);
    }
    
    public function update($uid, $data) {
        if ($data['password']) {
            $data['password'] = cmd5($data['password'], $this->_md5key);
        } else {
            unset($data['password']);
        }
        
        if ($data['nickname']) {
            $data['nickname'] = filter_utf8mb4($data['nickname']);
        }
        
        return parent::update($uid, $data);
    }
    
    public function updatePassword($id, $old, $new) {
        $where = array(
            $this->_id => $id,
            'password' => cmd5($old, $this->_md5key)
        );
        
        $password = $this->get($id, 'password');
        
        if ($password == cmd5($new, $this->_md5key)) {
            return true;
        }
        
        $data = array('password' => cmd5($new, $this->_md5key));
        
        $result = Db()->update(static::$table, $data, $where);
        
        if ($result) {
            return Db()->affectedRows();
        }
        return $result;
    }
    
    public function updatePasswordForce($id, $new) {
        $where = array($this->_id => $id);
        
        $data = array('password' => cmd5($new, $this->_md5key));
        
        return Db()->update(static::$table, $data, $where);
    }
    
    /**
     * 验证用户是否通过授权
     */
    public function checkLogin($username, $password) {
        $where = array(
            'username' => $username,
            'password' => cmd5($password, $this->_md5key),
            'locked' => 0,
        );
        return Db()->getOne(static::$table)->fields($this->_id)->where($where)->result();
    }
    
    public function getUidByUsername($username) {
        $where = array('username' => $username);
        $result = Db()->getOne(static::$table)->fields($this->_id)->where($where)->result();
        return $result;
    }
    
    public function searchUids($keyword) {
        $keyword = Db()->escape($keyword, false);
        $where = "username like '%{$keyword}%' or mobile like '%{$keyword}%' or realname like '%{$keyword}%' or nickname like '%{$keyword}%' or idcard like '%{$keyword}%'";
        return Db()->getCol(static::$table)->fields('uid')->where($where)->result();
    }
    
    public function searchUid($keyword) {
        $keyword = Db()->escape($keyword, false);
        $where = "username like '%{$keyword}%' or mobile like '%{$keyword}%' or realname like '%{$keyword}%' or nickname like '%{$keyword}%' or idcard like '%{$keyword}%'";
        return Db()->getOne(static::$table)->fields('uid')->where($where)->result();
    }
    
    /**
     * 输出用户信息
     * @param string $uid
     * @return array
     */
    public function getInfo($uid) {
        $where = array('uid' => $uid);
        $fields = array('username', 'realname', 'nickname', 'idcard', 'sex', 'mobile', 'avatar', 'add_time');
        $result = Db()->getRow(static::$table)->fields($fields)->where($where)->result();
        return $result;
    }
    
    public function existUser($username) {
        $where = array('username' => $username);
        $result = Db()->getRow(static::$table)->where($where)->result();
        return $result;
    }
    
    public function lockOne($uid) {
        $where = array($this->_id => $uid);
        $arrSets = array('locked' => 1);
        $result = Db()->update(static::$table, $arrSets, $where);
        return $result;
    }
    
    public function unlockOne($uid) {
        $where = array($this->_id => $uid);
        $arrSets = array('locked' => 0);
        $result = Db()->update(static::$table, $arrSets, $where);
        return $result;
    }
    
}
